package AppiumStudy;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

public class Itest {
    /**
     * 构造方法初始化加载浏览器驱动路径
     **/
    Itest(){
        System.setProperty("webdriver.chrome.driver","D:\\Software\\driver\\chromedriver.exe");
    }

    public static void main(String[] args) throws InterruptedException {
        Itest itest = new Itest();
        WebDriver driver = new ChromeDriver();
//        //get到百度首页
//        driver.get("https://www.baidu.com");
//
//        //窗口最大化
//        driver.manage().window().maximize();
//        Thread.sleep(2000);
//
//        driver.get("https://m.baidu.cn");
//        //设置窗口尺寸
//        driver.manage().window().setSize(new Dimension(480,800));


//        driver.get("https://www.baidu.com");
//        System.out.printf("进入 %s \n", driver.getCurrentUrl());
//
//        driver.findElement(By.linkText("新闻")).click();
//        System.out.printf("进入 %s \n", driver.getCurrentUrl());
//
//        driver.findElement(By.linkText("游戏")).click();
//        System.out.printf("进入 %s \n", driver.getCurrentUrl());
//
//        driver.navigate().refresh();
//        System.out.printf("刷新 %s \n", driver.getCurrentUrl());
//
//        driver.navigate().back();
//        System.out.printf("返回 %s \n", driver.getCurrentUrl());
//
//        driver.navigate().forward();
//        System.out.printf("前进到 %s \n", driver.getCurrentUrl());
//        String title = driver.getTitle();
//        System.out.println(title);

//        driver.get("https://www.baidu.com/");
//
//        WebElement search_text = driver.findElement(By.id("kw"));
//        WebElement search_button = driver.findElement(By.id("su"));
//
//        search_text.sendKeys("Java");
//        Thread.sleep(2000);
//        search_text.clear();
//        Thread.sleep(2000);
//        search_text.sendKeys("Selenium");
//        Thread.sleep(2000);
//        search_button.click();
////        search_text.submit();
//        Thread.sleep(2000);

//        driver.get("https://www.baidu.com");
//
//        WebElement size = driver.findElement(By.id("kw"));
//        System.out.println(size.getSize());
//
//        WebElement text = driver.findElement(By.id("cp"));
//        System.out.println(text.getText());
//
//        WebElement ty = driver.findElement(By.id("kw"));
//        System.out.println(ty.getAttribute("type"));
//
//        WebElement display = driver.findElement(By.id("kw"));
//        System.out.println(display.isDisplayed());

//        driver.get("https://www.baidu.com");
//
//        WebElement search_setting = driver.findElement(By.linkText("设置"));
//        Actions actions = new Actions(driver);
//        actions.clickAndHold(search_setting).perform();
//        Thread.sleep(2000);

//        driver.get("https://www.baidu.com");
//        System.out.println("Search before================");
//
//        System.out.printf("title of current page is %s\n", driver.getTitle());
//        System.out.printf("url of current page is %s\n", driver.getCurrentUrl());
//
//        WebElement search = driver.findElement(By.id("kw"));
//        search.sendKeys("Selenium");
//        search.sendKeys(Keys.ENTER);
//        Thread.sleep(2000);
//
//        System.out.println("Search after================");
//
//        System.out.printf("title of current page is %s\n", driver.getTitle());
//        System.out.printf("url of current page is %s\n", driver.getCurrentUrl());
//
//        WebElement result = driver.findElement(By.xpath("//div[@id='content_left']/div/h3/a"));
//        System.out.println(result.getText());

//        driver.get("https://www.baidu.com");
//
//        WebDriverWait wait = new WebDriverWait(driver, 10, 1);
//        wait.until(new ExpectedCondition<WebElement>() {
//            @Override
//            public WebElement apply(WebDriver text) {
//                return text.findElement(By.id("kw"));
//            }
//        }).sendKeys("selenium");
//
//        driver.findElement(By.id("su")).click();
//        Thread.sleep(2000);

//        driver.get("https://www.baidu.com");
//
//        driver.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS);
//        driver.findElement(By.id("kw")).sendKeys("selenium");
//
//        driver.manage().timeouts().setScriptTimeout(3, TimeUnit.SECONDS);
//

//        driver.get("https://www.baidu.com/");
//
//        WebElement search_text = driver.findElement(By.id("kw"));
//        search_text.sendKeys("selenium");
//        search_text.submit();
//        Thread.sleep(2000);
//
//        //匹配第一页搜索结果的标题， 循环打印
//        List<WebElement> search_result = driver.findElements(By.xpath("//div/div/h3"));
//
//        //打印元素的个数
//        System.out.println(search_result.size());
//
//        // 循环打印搜索结果的标题
//        for(WebElement result : search_result){
//            System.out.println(result.getText());
//        }
//
//        System.out.println("-------我是分割线---------");
//
//        //打印第n结果的标题
//        WebElement text = search_result.get(search_result.size() - 10);
//        System.out.println(text.getText());

//        driver.get("https://www.baidu.com");
//
//        Cookie c1 = new Cookie("name", "key-aaaaaaa");
//        Cookie c2 = new Cookie("value", "value-bbbbbb");
//        driver.manage().addCookie(c1);
//        driver.manage().addCookie(c2);
//
//        //获得 cookie
//        Set<Cookie> coo = driver.manage().getCookies();
//        System.out.println(coo);

        driver.get("https://www.baidu.com");

        File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        try {
            FileUtils.copyFile(srcFile, new File("d:\\scrshot.png"));
        }catch (IOException e){
            e.printStackTrace();
        }

        driver.quit();
    }

}
